// pages/fenzhen/fenzhen.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uqid:'',
    list:[],
    latitude:"",
    longitude:"",
    page:'1',
    limit:'10',
    maxpage:"",
    count:'',



    yijiindex: 0,
    erjiindex: -1,
    jibieindex: -1,
    paixuindex: 1,
    guanggao:'',
    showtc: false,
    jiazaizhong:'-1',
  },

    // 选地区
    select: function (e) {
      var index = e.currentTarget.dataset.index;
      if (this.data.showtc == false) {
        this.setData({
          select: Number(index)
        })
        this.setData({
          showtc: true
        })
      } else {
        if(this.data.select== Number(index)){
          this.setData({
            showtc: false
          })
        }else{
          this.setData({
            select: Number(index)
          })
          this.setData({
            showtc: true
          })
        }
      }
    },

    selectdiqu: function (e) {
      var index = e.currentTarget.dataset.index;
      this.setData({
        yijiindex: index,
        erjiindex: -1,
  
      })
  
    },
  
    // 选择级别
    selectjb: function (e) {
      var index = e.currentTarget.dataset.index;
      if (index == -1) {
  
      }
  
      this.setData({
        jibieindex: Number(index),
        detailsindex: '',
      })
     
      this.setData({
        showtc: false
      })
     
      this.getlist()
    },
  
    // 选择排序
    selectpx: function (e) {
      var index = e.currentTarget.dataset.index;
      this.setData({
        paixuindex: Number(index),
        currentpage: 1,
        nogd: false,
        canjiazai: true,
      })
      this.setData({
        showtc: false
      })
  
  
      this.getlist()
    },
    //选择具体地区
  selectjtdiqu: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      erjiindex: Number(index),
      detailsindex: '',
      currentpage: 1,
      nogd: false,
      canjiazai: true,
    })
    this.setData({
      showtc: false
    })
    this.getlist()
  },

  getguanggao: function () {
    ajax("/api/index/config_show", {
      data: {
        key:'fz_image'
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {

      this.setData({
        guanggao: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getguanggao()
    this.getfenleilist()
    this.getalldiqu()
  },

  
  getalldiqu: function () {
    ajax("/api/index/area_list", {
      data: {},
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {

      var obj = {
        name: '全部地区',
        province_id: "1",
        list: [{
          name: "全部地区",
          city_id: "1"
        }]
      }
      res.data.unshift(obj)

      this.setData({
        alldiqu: res.data
      })
    })
  },
   // 获取全部分类
   getfenleilist: function () {
   

    ajax("/api/index/job_list", {
      data: {
        
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      this.setData({
        fenleilist: res.data
      })
     
    })
  },

  //立即获取智能康复计划  && 授权
  bindGetUserInfo(e) {
    var that = this;
    console.log(e.detail.userInfo)
    var id = e.target.dataset.id
    console.log(id)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      if (that.data.islogin == false) {
        var code = wx.getStorageSync("code");
        ajax('/api/index/login', {
          data: {
            js_code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
        }, res => {
          console.log(res)
          if (res.error == 0) {
            wx.setStorageSync("uqid", res.data.uqid)
            that.setData({
              uqid: res.data.uqid,
              session_key: res.data.session_key
            });
            wx.navigateTo({
              url: '../details/details?id='+id +'&type=2',
            })

          } else {
            wx.showToast({
              title: res,
              icon: 'none',
              duration: 2000
            })

          }
        })
      }else{
        wx.navigateTo({
          url: '../details/details?id='+id +'&type=2',
        })
      }
    } else {
      
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  lower(){
    console.log('触底了')
    console.log('触底了')
        var page=Number(this.data.page)+1 
				this.setData({
          page:page
        })
				if(this.data.page>this.data.maxpage){
					
					wx.showToast({
						icon:"none",
						title: '没有更多了',
						duration: 2000,
					})
				}else{
					let that = this
					wx.showLoading({
						title: '加载中...'
					});
					
					that.getlist()
					
				}
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var uqid = wx.getStorageSync('uqid');
    this.setData({
      uqid: uqid,
      list:[]
    })
    if (uqid == '' || uqid == undefined) {
      this.setData({
        islogin: false
      });
    } else {
      this.setData({
        islogin: true
      });
    }

    var that=this
    var latitude= wx.getStorageSync('latitude')
    var longitude=wx.getStorageSync('longitude')

    if(latitude){
      that.setData({
        latitude: wx.getStorageSync('latitude'),
        longitude: wx.getStorageSync('longitude')
      })
      that.getlist()
    }else{
      wx.getLocation({
        type: 'wgs84',
        success: (res) => {
          var latitude = res.latitude
          var longitude = res.longitude
    
          wx.setStorageSync("latitude", latitude)
          wx.setStorageSync("longitude", longitude)
          that.setData({
            latitude: wx.getStorageSync('latitude'),
            longitude: wx.getStorageSync('longitude')
          })
          that.getlist()
        },
        fail: (res) => {
          that.getlist()
        },
      })
    }
  },

  getlist: function () {
    this.setData({
      jiazaizhong:true
    })

    var yijiindex = this.data.yijiindex
    var erjiindex = this.data.erjiindex
    var province_id
    var city
    var mtid
    var that=this

    if (yijiindex == 0) {
      province_id = ''
      city = ''
    } else if (erjiindex == -1) {
      province_id = that.data.alldiqu[yijiindex].province_id
      city = ''
    } else {

      province_id = that.data.alldiqu[yijiindex].province_id
      city = that.data.alldiqu[yijiindex].list[erjiindex].city_id
    }

    if(that.data.jibieindex==-1){
      mtid= ''
    }else{
      mtid= that.data.fenleilist[that.data.jibieindex].id
    }

    ajax("/api/index/experts_list", {
      data: {
        lng: that.data.longitude,
        lat: that.data.latitude,

        province_id: province_id,
        city: city,
        jid:mtid,
        sort: that.data.paixuindex,
        
      },
      header: {
        'Accept': 'application/json', // 默认值
      },
      method: 'get'
    }, res => {
      if(res.error==0){
        
        this.setData({
          jiazaizhong:false
        })
        if(res.data.list){
          var list=res.data.list
          
          that.setData({
            list: list
          })
          
        }else{
          that.setData({
            list: []
          })
        }
        wx.hideLoading();
        
      }
     
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      page:1,
      list:[],
      showtc:false,
      jiazaizhong:'-1'
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})